package com.chrissy.music.controller;

import com.chrissy.music.common.request.SysLoginRequest;
import com.chrissy.music.common.result.Result;
import com.chrissy.music.system.service.SysLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Johnson
 */
@RestController
@Tag(name = "登录", description = "登录")
@RequiredArgsConstructor
public class SysLoginController {

    private final SysLoginService sysLoginService;

    /**
     * 登录方法
     *
     * @param sysLoginRequest 登录信息
     * @return 结果
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody SysLoginRequest sysLoginRequest) {
        // 生成令牌
        String token = sysLoginService.login(sysLoginRequest);
        return Result.success(token);
    }

    @PostMapping("/logout")
    @Operation(summary = "退出")
    public Result<String> logout(HttpServletRequest request) {
        sysLoginService.logout(request);

        return Result.success();
    }

}
